package com.saile.controller.admin.media;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.saile.controller.base.BaseController;
import com.saile.def.Constant;
import com.saile.domain.Album;
import com.saile.domain.Media;
import com.saile.result.ResultObj;
import com.saile.service.AlbumService;
import com.saile.service.MediaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2017-11-15.
 */
@RequestMapping("/admin/media")
@Controller
public class AdminMediaController extends BaseController {
    @Autowired
    MediaService mediaService;
    @Autowired
    AlbumService albumService;

    /**
     * 获取专辑列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @RequestMapping("/getAlbumList")
    public ModelAndView getNewsList(Integer page, Integer pageSize) {
        ModelAndView m = new ModelAndView("/admin/media/albumList");
        Map<String, String> map = this.showRequestParameters();
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        map.put("page", String.valueOf(page));
        map.put("pageSize", String.valueOf(pageSize));
        PageHelper.startPage(page, pageSize);
        List<Album> albumList = albumService.getAlbumList(map);
        PageInfo<Album> pageInfo = new PageInfo<>(albumList);
        m.addObject("pageInfo", pageInfo);
        m.addObject("bean", map);
        return m;
    }

    /**
     * 编辑专辑提交
     *
     * @return
     */
    @RequestMapping(value = "/album")
    public ModelAndView news(Long tid) {
        ModelAndView m = new ModelAndView("/admin/media/album");
        if (tid != null) {
            Map<String, String> map = new HashMap<>();
            map.put("tid", String.valueOf(tid));
            Album album = albumService.getAlbum(map);
            m.addObject("album", album);
        }

        return m;
    }


    /**
     * 更新专辑
     *
     * @param album
     * @return
     */
    @RequestMapping(value = "/modifyAlbum")
    @ResponseBody
    public String modifyNews(Album album) {
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        try {
            albumService.modifyAlbum(album);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        logger.info(resultObj);
        return resultObj.toJson();
    }




    /**
     * 获取专辑列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @RequestMapping("/getMediaList")
    public ModelAndView getMediaList(Integer albumId,Integer page, Integer pageSize) {
        ModelAndView m = new ModelAndView("/admin/media/mediaList");
        Map<String, String> map = this.showRequestParameters();
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        map.put("page", String.valueOf(page));
        map.put("pageSize", String.valueOf(pageSize));
        if(albumId!=null){
            map.put("albumId", String.valueOf(albumId));
        }
        PageHelper.startPage(page, pageSize);
        List<Media> mediaList = mediaService.getMediaList(map);
        PageInfo<Media> pageInfo = new PageInfo<>(mediaList);
        m.addObject("pageInfo", pageInfo);
        m.addObject("bean", map);
        return m;
    }



    /**
     * 编辑专辑提交
     *
     * @return
     */
    @RequestMapping(value = "/media")
    public ModelAndView media(Long tid,Long albumId) {
        ModelAndView m = new ModelAndView("/admin/media/media");
        if (tid != null) {
            Map<String, String> map = new HashMap<>();
            map.put("tid", String.valueOf(tid));
            Media media = mediaService.getMedia(map);
            m.addObject("media", media);
            if(media!=null){
                albumId=media.getAlbumId();
            }
        }
        logger.info("albumId====>>>"+albumId);
        if(albumId!=null){
            m.addObject("albumId", albumId);
        }
        return m;
    }

    /**
     * 更新专辑
     *
     * @param media
     * @return
     */
    @RequestMapping(value = "/modifyMedia")
    @ResponseBody
    public String modifyMedia(Media media, MultipartFile location) {
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        String mediaPicUrl = "";
        try {

            try {
                String filePath = upLoadFile(location);
                mediaPicUrl = filePath;
                logger.info("resourceUrl=============>>>" + mediaPicUrl);
                media.setMediaPic(mediaPicUrl);
            } catch (Exception e) {
                e.printStackTrace();
            }

            mediaService.modifyMedia(media);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }
@RequestMapping(value = "/deleteMedia")
    @ResponseBody
    public String deleteMedia(Long mediaId) {
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        try {
            Map<String,String> map=new HashMap<>();
            map.put("tid",String.valueOf(mediaId));
            mediaService.deleteMedia(map);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }
@RequestMapping(value = "/deleteAlbum")
    @ResponseBody
    public String deleteAlbum(Long albumId) {
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        try {
          albumService.deleteAlbumById(albumId);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }


}
